package com.keeping.aiimagery.entity;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class Sms {

    private static String AccessKey= "LTAI5tSo5t5gB4n4TzLXWnRQ";
    private static String AccessSecret= "zPyCXzv1L5mmtHVvOFAnowm6dmTQtd";
    private static String signName= "Eli之页";
    private static String TempletCode="SMS_276335433";
    private static int code;


    public static boolean sendSmsUtil(String phoneNumber) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKey, AccessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //自定义信息
        request.putQueryParameter("PhoneNumbers", phoneNumber); //发送至手机号
        request.putQueryParameter("SignName", signName);  //自己配置的短信签名
        request.putQueryParameter("TemplateCode", TempletCode); //自己配置的模板 模版CODE

        //构建一个短信验证码
        code = (int) ((Math.random() * 9 + 1) * 1000);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");   //转换成json字符串
        try {
            CommonResponse response = client.getCommonResponse(request); //发送至客户端
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();//返回是否发送成功
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return true;

    }
    public static int getCode()
    {
        return code;
    }
}
